<?php
App::import('Sanitize');
class CategoriesController extends AppController {
	var $name = 'Categories';
	var $uses = array('Product', 'Category');
	var $helpers = array('Html', 'Session','Thumb');
	var $components = array('Auth','Seo');

	function beforeFilter() {
		parent::beforeFilter();
	}
	function beforeRender() {
		@$this->set('meta_title', 'Categories');
		@$this->set('meta_description', 'Categories');
		@$this->set('meta_keywords', 'Categories');
	}

	function index() {
		$user = $this->Auth->user();
		if ($user['User']['role'] == 1) {
			$this->Session->setFlash('Invalid url');
			$this->redirect('/');
		}
		$categories = $this->Category->find('all', array('order' => 'id'));
		$this->set(compact('categories'));
	}

	function add() {
		$user = $this->Auth->user();
		if ($user['User']['role'] == 1) {
			$this->Session->setFlash('Invalid url');
			$this->redirect('/');
		}
		if(!$user || $user['User']['role'] > 0){
			$this->redirect('/');
		}
		$this->set(compact('back_link'));
		if (!empty($this->data)) {
			$this->Category->create();
			$this->data['Category']['user_id']=$user['User']['id'];
			if ($this->Category->save($this->data)) {
				$url='/categories/index/';
				$this->Session->setFlash(__('The category has been saved', true));
				$this->redirect($url);
			}
			else {
				$this->Session->setFlash(__('The category could not be saved. Please, try again.', true));
			}
		}
	}

	function delete($id) {
		$user = $this->Auth->user();
		if ($user['User']['role'] == 1) {
			$this->Session->setFlash('Invalid url');
			$this->redirect('/');
		}
		$category=$this->Category->findById($id);
		$current_user=$this->Auth->user();
		if($current_user&&$current_user['User']['role']==ADMIN){
		}
		else{
			$this->Session->setFlash('Cannot delete category.');
			$this->redirect('/categories');
		}
		if($this->Category->delete($id)){
			$this->Session->setFlash('The category has been deleted.');
			$this->redirect(array('controller' => 'categories', 'action' => 'index'));
			
		}
		else{
			$this->Session->setFlash('Cannot delete category.');
			$this->redirect($url);
		}
	}
}
?>
